import 'dart:io';

import 'package:app/generated/r.dart';
import 'package:app/page/mine/mine_page.dart';
import 'package:app/page/whiteboard_page.dart';
import 'package:app/utils/shared_preferences_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_jpf_core/network/http_manager.dart';
import 'package:flutter_jpf_core/page/base_state_page.dart';
import 'package:flutter_jpf_core/util/colors_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lazy_indexed_stack/lazy_indexed_stack.dart';

import 'home/home_page.dart';




class IndexPage extends CommBasePage {
  @override
  CommBaseState<CommBasePage> getPageState() {
    return IndexPageState();
  }

}


class IndexPageState extends CommBaseState<IndexPage>
    with WidgetsBindingObserver {

  static IndexPageState _currenState;

  static IndexPageState getCurrenState(){
    return _currenState;
  }

  List<Widget> pages = [];
  int _currentIndex = 0;

  void _onItemTapped(int index) {
    if (mounted) {
      setState(() {
        _currentIndex = index;
      });
    }
  }

  @override
  void initBaseParams() {
    _currenState = this;
    HttpManager.getHeaders = (){
      return {};
    };

    WidgetsBinding.instance.addObserver(this);
    pages.add(HomePage());
    pages.add(MinePage());
    // pages.add(Container(color: Colors.black12,));
    // pages.add(Container(color: Colors.green,));
  }

  @override
  void initPageData() {
    hideKeyboard();
  }


  @override
  Widget getViewsWidget() {
    ScreenUtil.init(context,width: 750/2, height: 1334/2);
    List<BottomNavigationBarItem> items = [
      BottomNavigationBarItem(
          icon: getTabIcon(_currentIndex)[0], label: '首页'),
      BottomNavigationBarItem(
          icon: getTabIcon(_currentIndex)[1], label: '我的'),
    ];

    return WillPopScope(
      onWillPop: doubleClickBack,
      child: Scaffold(
        bottomNavigationBar: BottomNavigationBar(
          items: items,
          currentIndex: _currentIndex,
          onTap: _onItemTapped,
          type: BottomNavigationBarType.fixed,
          //有 fixed 和 shifting 两种类型
          unselectedItemColor: ColorsUtil.black_333,
          selectedItemColor: ColorsUtil.blue_dark1,
          selectedFontSize: 12.0,
          unselectedFontSize: 12.0,
        ),
        body: LazyIndexedStack(
          reuse: false,
          index: _currentIndex,
          itemBuilder: (c, i) {
            return pages[_currentIndex];
          },
          itemCount: pages.length,
        ),
      ),);
  }

  int last = 0;

  Future<bool> doubleClickBack() {
    int now = DateTime
        .now()
        .millisecondsSinceEpoch;
    if (now - last > 1000) {
      last = DateTime
          .now()
          .millisecondsSinceEpoch;
      showToastTip("再按一次返回键退出");
      return Future.value(false);
    } else {
      return Future.value(true);
    }
  }

  List<Image> getTabIcon(int index) {
    double imgWidth = 20;
    double imgHeight = 20;

    var imvArr = [
      Image.asset(index == 0 ? R.img_tab_home1 : R.img_tab_home2,
          fit: BoxFit.fill,
          width: imgWidth,
          height: imgHeight),
      Image.asset(index == 1 ? R.img_tab_me1 : R.img_tab_me2,
          fit: BoxFit.fill,
          width: imgWidth,
          height: imgHeight)
    ];
    return imvArr;
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }

}